<template>
  <dept-user-select :visible="visible" @afterSelect="confirmAssign" @cancel="cancelAssign">
    <template slot="form">
      <el-form ref="form" :model="form" class="opinion-form">
        <el-form-item label="转办意见">
          <el-input type="textarea" :rows="3" placeholder="请输入转办意见" v-model="form.opinion"></el-input>
        </el-form-item>
      </el-form>
    </template>
  </dept-user-select>
</template>

<script>
  import {getTwoUnit,getUsersByUnitId,getDeptTree} from "@/api/system";
  import {taskAssignee} from "@/api/monitor";
  import {mapGetters} from "vuex";

  export default {
    name: "assign",
    props: {
      visible: {
        type: Boolean,
        default: true,
      },
      taskId:{
        type: String,
        default: null,
      }
    },
    data() {
      return {
        form:{},
        userIdTemp:null,
      };
    },
    computed: {
      ...mapGetters([
        "deptInfo"
      ])
    },
    methods: {
      cancelAssign(){
//        this.visible = false;
        this.$emit('cancelAssign',this.userIdTemp)
      },
      confirmAssign(userIds){
        if(userIds != null && userIds.length > 1){
          this.$notify({
            title: "提醒",
            type: "warning",
            message: "只能选择一个转办人员",
            duration: 2000
          });
          return false;
        }
        if (userIds == null) {
          this.$notify({
            title: "提醒",
            type: "warning",
            message: "请选择转办人员",
            duration: 2000
          });
          return;
        }
        let userId = userIds[0];
        let data = {
          userId: userId,
          opinion: this.form.opinion
        }
        if (this.taskId != '' && this.taskId != null && this.taskId != undefined) {
          this.$confirm("确定转办吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            taskAssignee(this.taskId, data)
                    .then(response => {
                      if (response.data.code == '0') {
                        this.$message({
                          message: '转办成功！',
                          type: 'success'
                        });
                        this.$emit('confirmAssign', userId);
                        this.$router.go(-1);
                      }
                    })
          });
        }
      },
    },
  }
</script>

<style lang="scss" scoped>
  /deep/.opinion-form .el-form-item__content{
    width: 100%
  }

</style>
